#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMenuBar>
#include <QFile>
#include <QPainter>
#include "mypushbutton.h"
#include <QDebug>
#include <QTimer>
#include <QSound>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{

    ui->setupUi(this);
    /* 设置主窗口大小 */
    this->setFixedSize(QSize(320, 588));
    /* 设置窗口图标 */
    this->setWindowIcon(QIcon(":/Image/Coin0001.png"));
    /* 设置窗口标题 */
    this->setWindowTitle("翻金币游戏");
    /* 获取菜单栏 */
    QMenuBar *mBar = menuBar();
    /* 创建菜单 */
    QMenu *startMenu = new QMenu(this);
    startMenu->setTitle("开始");
    /* 添加菜单 */
    mBar->addMenu(startMenu);
    /* 添加菜单项 */
    QAction *backAction = new QAction(this);
    backAction->setText("退出");
    startMenu->addAction(backAction);
    /* 做出退出功能 */
    connect(backAction, &QAction::triggered, this, &QMainWindow::close);
    /* 做开始按钮 */
    MyPushButton *startButton = new MyPushButton(":/Image/MenuSceneStartButton.png");
    startButton->setParent(this);
    startButton->move((this->width() - startButton->width()) * 0.5, (this->height() * 0.7));
    /* 申请对象 */
    this->chooseLevel = new ChooseLevel(this);
    /* 做开始按钮的功能 */
    connect(startButton, &MyPushButton::clicked, this, [=](){
//        qDebug() << "start button is pressed";
        /* 音效 */
        QSound *sound = new QSound(":/Image/TapButtonSound.wav", this);
        sound->play();
        /* 设置播放循环 */
//        sound->setLoops(10);
        /* 显示动画效果 */
        startButton->jump_up();
        startButton->jump_down();
        /* 实现延时效果，0.5秒后运行界面切换, 500毫秒后this执行槽函数 */
        QTimer::singleShot(500, this, [=](){
            /* 隐藏本界面 */
            this->hide();
            /* 显示关卡显示界面 */
            this->chooseLevel->show();
        });
    });
    /* 监听返回信号 */
    connect(this->chooseLevel, &ChooseLevel::backButtonClicked, this,
            [=](){
        /* 做一个延时的效果 */
        QTimer::singleShot(500, this, [=](){
            this->chooseLevel->hide();
            this->show();
        });
    });
}

void MainWindow::paintEvent(QPaintEvent *)
{
    /* 画背景 */
    QPainter painter(this);

    QPixmap pix;
    pix.load(":/Image/PlayLevelSceneBg.png");
    /* 画背景宽和高 */
    painter.drawPixmap(0, 0, this->width(), this->height(), pix);
    /* 画背景图标 */
    pix.load(":/Image/Title.png");
    /* 按比例缩放图标 */
    pix = pix.scaled(pix.width() * 0.5, pix.height() * 0.5);
    painter.drawPixmap(10, 30, pix);
}

MainWindow::~MainWindow()
{
    delete ui;
}

